clear

local colist HU   SE   RO PT PL NO  IT  GB FR  ES DK DE CZ BG    AT
local numberco : word count `colist'
local firstco: word 1 of `colist'

foreach nuts in 2 3{

** 1000 iterations
local RR=1000

set seed 23423

**Country loop starts here
foreach co in `colist' {


di "Country is `co'"

use "$BvD_input/Amadeus_Europe_2010_2019_coded_latest_NUTS`nuts'_`co'.dta", clear

**Create ordering variable
quietly gen pid	=_n

**A robustness check uses FIAS (fixed assets) as capital measure for TFP. No big differences and not reported in paper, so we use a filler for now in its place. Change code to conduct this robustness check:
*quietly ren (w1_lvapw w1_log_TFPr_fias w1_log_TFPr_tfas) (lprod_1 lprod_2 lprod_3)
quietly ren w1_lvapw  lprod_1
quietly ren w1_log_TFPr_tfas  lprod_2 
quietly gen lprod_3 = lprod_1


tempfile AA BB
quietly save `AA'

*Create within-industry ordering variable (will be used later for permutation)
bys  T J (pid) : gen ind_pid = _n
*This is a skeleton of location-plant-industry counts with IDs that we will randomly assign
*plants from the nation-wide industry population below
quietly keep  S T J ind_pid
quietly save `BB'

forvalues rr=0(1)`RR' {
**Note: first (rr=0) iteration of the 1001 permutations will contain TRUE values; 
**the remaining rr={1,2,...,RR=1000} will be permutations.

if `rr'==0 {
	tempfile C1 C2 C3
	*Loop through the productivity variables (indexed by nn) defined above
	forvalues nn=1(1)3 {
		use `AA', clear

	*Permutations are done in this worker file
	run "$BvD_code/Permute_worker.do" `nn'
		quietly keep if _n==1 
		quietly keep N_* var* 
		quietly save `C`nn''
	}

	*Merge statistics across productivities (nn) within a country
	use `C1',clear
	quietly cap drop _m
	quietly merge 1:1 _n using `C2', nogen
	quietly merge 1:1 _n using `C3', nogen
	quietly gen co		= "`co'"
	quietly gen iter	= `rr'
	quietly save "$BvD_output/permute_out_stats_`co'`nuts'.dta", replace
} 
*Now generate 1000 permutations (above was "true" economy)
else {
	use `AA', clear
	
	
	*Scramble within-industry plant ID (contrast with "true" within-inudstry PID above stored in file BB)
	*Ultimatley, this amounts to scrambling location S
	quietly gen randu = runiform()
	quietly bys  T J (randu) : gen ind_pid = _n
	quietly ren S true_location
	*Merge on peer industry plant skeleton (defined above; preserving plant-industry-location counts)
	quietly merge 1:1  T J ind_pid using `BB'
	tempfile AB
	quietly save `AB'

*Now run same code as above to generate country-specific statistics, for each permutation
	tempfile C1 C2 C3
	forvalues nn=1(1)3 {
		use `AB', clear
		
		run "$BvD_code/Permute_worker.do" `nn'
		quietly keep if _n==1 
		quietly keep N_* var* 
		quietly save `C`nn''
	}

	use `C1',clear
	quietly cap drop _m
	quietly merge 1:1 _n using `C2', nogen
	quietly merge 1:1 _n using `C3', nogen
	
	quietly gen co		= "`co'"
	quietly gen iter	= `rr'
	
	quietly append using "$BvD_output/permute_out_stats_`co'`nuts'.dta"
	quietly save "$BvD_output/permute_out_stats_`co'`nuts'.dta", replace
	
}
}

}
 
}
